package com.giec.autoble.receiver;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.giec.autoble.R;
import com.giec.autoble.service.BleService;

public class BootReceiver extends BroadcastReceiver {
    public static final String TAG = "BootReceiver";

    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: 1");
        openBLE();


        //适配8.0service
//        NotificationManager notificationManager = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
//        NotificationChannel mChannel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//            mChannel = new NotificationChannel("1", context.getString(R.string.app_name),
//                    NotificationManager.IMPORTANCE_LOW);
//            notificationManager.createNotificationChannel(mChannel);
//            Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
//            context.startForegroundService(new Intent(context, BleService.class));
        }

        context.startForegroundService(new Intent(context, BleService.class));
    }

    private void openBLE() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled()) {
            bluetoothAdapter.enable();//异步的，不会等待结果，直接返回。
            Log.d(TAG, "未开启蓝牙，开启");
        }
    }
}